{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "转换原始数据至0-1矩阵\n",
      "转换完毕，用时1.40438205767秒\n",
      "\n",
      "开始搜索关联规则...\n",
      "\n",
      "正在进行第1次搜索...\n",
      "数目：190...\n",
      "\n",
      "正在进行第2次搜索...\n",
      "数目：586...\n",
      "\n",
      "正在进行第3次搜索...\n",
      "数目：18...\n",
      "\n",
      "结果为：\n",
      "Empty DataFrame\n",
      "Columns: [support, confidence]\n",
      "Index: []\n",
      "\n",
      "搜索完成，用时：1.72秒\n"
     ]
    }
   ],
   "source": [
    "# 2> 模型建立\n",
    "# -*- coding:utf-8 -*-\n",
    "# -*- coding:utf-8 -*-\n",
    "from __future__ import print_function\n",
    "import pandas as pd\n",
    "from pandas import DataFrame,Series\n",
    "from selfapriori import * # 导入自行编写的Apriori函数\n",
    "import time# 导入时间库来计算用时\n",
    "\n",
    "# inputfile ='apriori.txt' #输入事务集文件\n",
    "# '''apriori.txt中文件格式如下\n",
    "# A1,B2,C1,D3,E2,F1,H2\n",
    "# A2,B2,C1,D2,E2,F1,H3\n",
    "# A3,B4,C2,D3,E4,F1,H4\n",
    "# A3,B1,C2,D1,E1,F1,H1\n",
    "# '''\n",
    "# data2 = pd.read_csv(inputfile, header=None, dtype=object)\n",
    "data2 = pd.read_excel('apriori.xlsx', header=0)\n",
    "\n",
    "start = time.clock() # 计时开始\n",
    "print(u'\\n转换原始数据至0-1矩阵')\n",
    "\n",
    "ct = lambda x: Series(1, index = x[pd.notnull(x)]) # 将标签数据转换成1，是转换0-1矩阵的过渡函数\n",
    "b = map(ct, data2.as_matrix())# 用map方式执行\n",
    "data2 = DataFrame(b).fillna(0)\n",
    "end = time.clock() #计时开始\n",
    "\n",
    "print (u'转换完毕，用时%s秒' % (end-start))\n",
    "del b #删除中间变量b 节省内存\n",
    "\n",
    "support = 0.06 #最小支持度\n",
    "confidence = 0.75 #最小置信度\n",
    "ms = '---'# 用来区分不同元素，需要保证原始表格中无该字符\n",
    "\n",
    "start = time.clock() #计时开始\n",
    "print(u'\\n开始搜索关联规则...')\n",
    "find_rule(data2, support, confidence, ms)\n",
    "end = time.clock() \n",
    "print (u'\\n搜索完成，用时：%.2f秒' % (end-start))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
